from PySide6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QCheckBox, QFileDialog
from PySide6.QtCore import Signal

class LoggerPanel(QWidget):
    log_config_changed = Signal(str)
    
    def __init__(self, log_manager):
        super().__init__()
        self.log_manager = log_manager
        self.init_ui()
        self.setObjectName("logger_panel")
        
    def init_ui(self):
        layout = QVBoxLayout(self)
        
        # 日志开关
        self.enable_logging = QCheckBox('启用日志记录')
        self.enable_logging.setChecked(self.log_manager.is_logging_enabled())
        self.enable_logging.stateChanged.connect(self.on_logging_enabled_changed)
        layout.addWidget(self.enable_logging)
        
        # 日志保存路径
        path_layout = QHBoxLayout()
        path_label = QLabel('日志保存路径：')
        self.path_input = QLineEdit()
        self.path_input.setText(self.log_manager.get_log_dir())
        self.path_input.setReadOnly(True)
        self.browse_button = QPushButton('浏览')
        self.browse_button.clicked.connect(self.on_browse_clicked)
        
        path_layout.addWidget(path_label)
        path_layout.addWidget(self.path_input)
        path_layout.addWidget(self.browse_button)
        layout.addLayout(path_layout)
        
        # 连接日志管理器的信号
        self.log_manager.log_config_changed.connect(self.on_log_config_changed)
        
    def on_logging_enabled_changed(self, state):
        self.log_manager.set_logging_enabled(state == 2)  # Qt.Checked = 2
        
    def on_browse_clicked(self):
        dir_path = QFileDialog.getExistingDirectory(
            self,
            "选择日志保存路径",
            self.path_input.text()
        )
        if dir_path:
            self.log_manager.set_log_dir(dir_path)
            self.path_input.setText(dir_path)
            
    def on_log_config_changed(self, log_dir):
        self.log_config_changed.emit(log_dir)